﻿using System.Collections.Generic;
using OpenTK;

namespace Chimp.Core
{
    public class ChimpBase
    {
        public ChimpEffect BaseEffect;
        public Vector2 DrawPos = Vector2.Zero;
        public float DrawRotation;
        public float DrawScale;
        public int LayerID;
        public Dictionary<string, float> Levels = new Dictionary<string, float>();
        public List<ChimpBase> Nodes = new List<ChimpBase>();
        public ChimpBase Parent;
        public Vector2 Pos = Vector2.Zero;
        public ChimpRenderTarget RT;
        public int RTgutter;
        public ChimpTexture2D RTtex;
        public Dictionary<string, float> Ranges = new Dictionary<string, float>();
        public float Rotation;
        public float Scale = 1.0f;

        public ChimpBase(int rtw, int rth)
        {
            RT = new ChimpRenderTarget();
            RTtex = new ChimpTexture2D(rtw, rth);
            Size = new Vector2(rtw, rth);
            Scale = 1.0f;
        }

        public ChimpBase()
        {
            ID = "";
            Pos = Vector2.Zero;
            Scale = 1.0f;
            Rotation = 0.0f;
        }

        public string ID { get; set; }
        public string Author { get; set; }
        public string Copyright { get; set; }
        public ChimpScene Scene { get; set; }
        public Vector2 Size { get; set; }

        public float SetLevel(string id, float f, float r)
        {
            if (Levels.ContainsKey(id))
            {
                Levels[id] = f;
                Ranges[id] = r;
                return f;
            }
            Levels.Add(id, f);
            Ranges.Add(id, r);
            return f;
        }

        public float GetRange(string id)
        {
            if (Ranges.ContainsKey(id))
            {
                return Ranges[id];
            }
            return 0.0f;
        }

        public float GetLevel(string id)
        {
            if (Levels.ContainsKey(id))
            {
                return Levels[id];
            }
            return 0.0f;
        }

        public void SetBaseFX(string frag)
        {
            BaseEffect = new ChimpEffect("", frag + ".glsl");
        }

        public void SetBaseFX_Blur()
        {
            SetBaseFX("Res/Shader/BFX_Blur");
        }

        public void SetFX_Blur(float blur)
        {
            BaseEffect.SetInt("SourceMap", 0);
            BaseEffect.SetFloat("BlurRadius", blur);
        }

        public void BindRT()
        {
            RT.AttachTexture(RTtex);
            RT.Bind();
            RT.AttachTexture(RTtex);
            //   OpenTK.Graphics.OpenGL.GL.Clear(OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit | OpenTK.Graphics.OpenGL.ClearBufferMask.DepthBufferBit);
            if (BaseEffect != null)
            {
                //   BaseEffect.Use();
            }
        }

        public void UnbindRT()
        {
            if (BaseEffect != null)
            {
                // BaseEffect.Fin();
            }
            RT.Unbind();
        }

        public virtual void SetPos(Vector2 pos)
        {
            Pos = pos;
        }

        public void SetRot(float degrees)
        {
            Rotation = degrees;
        }

        public void SetScale(float scale)
        {
            Scale = scale;
        }

        public void Update(Vector2 pos)
        {
            OnUpdate(pos);
        }

        public virtual bool OnUpdate(Vector2 pos)
        {
            return false;
        }

        public void Draw(Vector2 pos)
        {
            if (RT != null)
            {
                ChimpDraw.BlendMode = ChimpBlend.Lighten;
                //ChimpDraw.Image(new Vector4(20, 20, 200, 200), RTtex, new Vector4(1, 1, 1, 1));
                //ChimpDraw.Rect(new Vector4(20, 20, 200, 200), new Vector4(1, 1, 1, 1));


                ChimpDraw.Image(new Vector4(DrawPos.X - Size.X/2, DrawPos.Y - Size.Y/2, Size.X, Size.Y), DrawRotation,
                                DrawScale, new Vector4(1, 1, 1, 1), RTtex, false);
            }
            else
            {
                OnDraw(pos);
                foreach (ChimpBase n in Nodes)
                {
                    n.Draw(pos);
                }
            }
        }

        public void Prerender(Vector2 pos)
        {
            Vector2 op = DrawPos;
            float or = DrawRotation;
            float os = DrawScale;
            if (RT != null)
            {
                BindRT();


                DrawPos = new Vector2(RTtex.Width/2, RTtex.Height + RTtex.Height/2);
                DrawRotation = 0.0f;
                DrawScale = 1.0f;


                //ChimpDraw.Rect(new Vector4(0,,256,256), new Vector4(1, 1, 1, 1));
                OnDraw(pos);


                UnbindRT();
                DrawPos = op;
                DrawRotation = or;
                DrawScale = os;
            }
        }

        public virtual void OnDraw(Vector2 pos)
        {
        }

        public virtual void OnDrawShape(Vector2 pos)
        {
        }
    }
}